package com.ymatou.iapi.sellerproduct.testcase;

import java.util.Arrays;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymatou.iapi.sellerproduct.service.GetProductTemplateCall;
import com.ymatou.iapi.sellerproduct.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.model.YmtProductDescExtra;
import com.ymttest.database.sqlwapper.YmtProductDescExtraWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_GetProductTemplate {
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static GetProductTemplateCall getProductTemplateCall = new GetProductTemplateCall();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取单个商品模版");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductTemplateCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductTemplate_001() {
		Logger.start(true, "获取单个商品模版");
		try{
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			Integer templateId = sizeTemplate.getTemplateId();			
			
			
			//template info in sql
			YmtProductDescExtraWapper ymtProductDescExtraWapper = new YmtProductDescExtraWapper();
			YmtProductDescExtra ymtProductDescExtra = ymtProductDescExtraWapper.selectByTemplateId(templateId);
			
			//template info in response
			JSONObject retBean = getProductTemplateCall.callAndGetReturnData(String.valueOf(templateId));
			
			Logger.verifyEquals(200, retBean.getInt("Code"), "报文返回值比对");
			
			checkRetOfProductTemplate(MapUtil.pojoToMap(ymtProductDescExtra),retBean.getJSONObject("Data"));
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	/***
	 * 
	 * @param templateInfosInSql sql中template值
	 * @param templateListInRet 接口请求返回的template值
	 * @throws JSONException 
	 */
	public static void checkRetOfProductTemplate(Map<String,Object> templateInfoInSql, JSONObject templateInRet) throws Exception{
		//图片检查	
		JSONArray requestPics = templateInRet.getJSONArray("PicUrl");
		if (templateInfoInSql.get("picUrls")==null || templateInfoInSql.get("picUrls").toString().equals("")){
			Logger.verifyEquals(0, requestPics.length(), "Template 图片应该不存在");
		}else{
			JSONArray templatePicsInfoInSql = new JSONArray(Arrays.asList(templateInfoInSql.get("picUrls").toString().split(",")));
			AssertService.checkArray(templatePicsInfoInSql, requestPics,"Template 图片检查 ");
		}
		Map<String, Object> requestMap = MapUtil.hashMap();
		requestMap.put("ID", templateInRet.getInt("ProductTemplateId"));
		requestMap.put("templateName", templateInRet.getString("TemplateName"));
		requestMap.put("description", templateInRet.getString("Description"));
		
		AssertService.assertResultEqual(requestMap, templateInfoInSql);
	}
}
